package com.yanqu.road.server.cmd.banquet.party;

import com.yanqu.road.entity.banquet.BanquetInfo;
import com.yanqu.road.entity.banquet.cross.CrossBanquetData;
import com.yanqu.road.entity.banquet.cross.CrossBanquetMember;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.banquet.CrossBanquetConfigMgr;
import com.yanqu.road.server.manager.banquet.CrossPartyMgr;
import com.yanqu.road.server.pb.banquet.CrossBanquetPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Map;

@Cmd(code = CrossProtocol.C_CROSS_PARTY_GET_REWARD, desc = "")
public class CrossPartyGetRewardCmd extends CrossCommand {
    private String beautiesSkillAddition;
    private int normalBanquetNum;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BanquetProto.CrossPartyGetRewardReqMsg req = BanquetProto.CrossPartyGetRewardReqMsg.parseFrom(packet.getBytes());
        beautiesSkillAddition = req.getBeautiesSkillAddition();
        normalBanquetNum = req.getNormalBanquetNum();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        long userId = packet.getPlayerId();
        int fillRobotNum = 0;
        Map<String, CrossBanquetData> dataMap = CrossPartyMgr.getOverPartyMap(serverId, userId);
        for (CrossBanquetData banquet : dataMap.values()) {
            if (CrossBanquetConfigMgr.isServerNormalBanquet(banquet.getType()) && normalBanquetNum + fillRobotNum < GameConfig.BANQUET_MARRY_FULL_SEAT_NUM) {
                CrossPartyMgr.fullRobotSeat(banquet);
                fillRobotNum++;
            }
            banquet.setSendReward(true);
            BanquetInfo banquetInfo = CrossBanquetConfigMgr.getBanquetInfo(banquet.getType());
            int addFullSeat = 0;
            if (banquet.getUserCount() == banquetInfo.getSeat()) {
                addFullSeat = banquetInfo.getFulPopularity();
            }
            int addEndEffect = banquetInfo.getEndEffect();
            BigDecimal popularity = BigDecimal.valueOf(banquet.getPopularity());
            BigDecimal integral = BigDecimal.valueOf(banquet.getIntegral());
            popularity = popularity.multiply((BigDecimal.valueOf(1000 + addFullSeat + addEndEffect))).divide(BigDecimal.valueOf(1000));
            integral = integral.multiply((BigDecimal.valueOf(1000 + addFullSeat))).divide(BigDecimal.valueOf(1000));

            //奖励中也会配置人气和积分奖励,需要加到宴会中去
            long rewardPopularity = 0;
            long rewardIntegral = 0;
            //宴会结束后的奖励
            if (!"0".equals(banquetInfo.getEndReward())) {
                String endRewardStr = CrossBanquetConfigMgr.getEndReward(banquet.getUserCount(), banquet.getType());
                Property endReward = PropertyHelper.parseStringToProperty(endRewardStr);
                //要把宴会和人气的奖励合并起来合到宴会里面去
                rewardPopularity = endReward.getCountByGoodsId(GameConfig.BANQUET_POPULARITY).longValue();
                rewardIntegral = endReward.getCountByGoodsId(GameConfig.BANQUET_INTEGRAL).longValue();
                endReward.removeProperty(GameConfig.BANQUET_POPULARITY, BigInteger.valueOf(rewardPopularity));
                endReward.removeProperty(GameConfig.BANQUET_INTEGRAL, BigInteger.valueOf(rewardIntegral));
                banquet.setEndReward(PropertyHelper.parsePropertyToString(endReward));
            }
            popularity = popularity.add(BigDecimal.valueOf(rewardPopularity));
            integral = integral.add(BigDecimal.valueOf(rewardIntegral));

            //只有本分私宴才有美女技能加成
            if (CrossBanquetConfigMgr.isServerNormalBanquet(banquet.getType())) {
                //美女技能加成
                popularity = popularity.
                        multiply(BigDecimal.valueOf(1000).add(new BigDecimal(beautiesSkillAddition))).
                        divide(BigDecimal.valueOf(1000));
            }

            integral = integral.divide(BigDecimal.valueOf(1), 0, BigDecimal.ROUND_UP);
            popularity = popularity.divide(BigDecimal.valueOf(1), 0, BigDecimal.ROUND_UP);
            if (banquetInfo.getId() == 10003 || banquetInfo.getId() == 10004) {
                integral = integral.multiply(BigDecimal.valueOf(GameConfig.BANQUET_PRIVATE_MULTI_INTEGRAL)).divide(BigDecimal.valueOf(1000), 0, BigDecimal.ROUND_UP);
                popularity = popularity.multiply(BigDecimal.valueOf(GameConfig.BANQUET_PRIVATE_MULTI_POPULARITY)).divide(BigDecimal.valueOf(1000), 0, BigDecimal.ROUND_UP);
            }

            banquet.setPopularity(popularity.longValue());
            banquet.setIntegral(integral.longValue());
            Map<Long, CrossBanquetMember> partyMemberMap = CrossPartyMgr.getPartyMemberMap(banquet.getBanquetId());
            banquet.setMemberMap(partyMemberMap);

            CrossPartyMgr.sendBanquetUserRecord(banquet);
        }


        BanquetProto.CrossPartyGetRewardRespMsg.Builder respMsg = BanquetProto.CrossPartyGetRewardRespMsg.newBuilder();
        for (CrossBanquetData banquet : dataMap.values()) {
            BanquetProto.BanquetGetRewardTempMsg.Builder temp = BanquetProto.BanquetGetRewardTempMsg.newBuilder();
            temp.setBanquetId(banquet.getBanquetId());
            temp.setType(banquet.getType());
            temp.setOpenTime(banquet.getOpenTime());
            temp.setInfo(banquet.getInvitationInfo());
            temp.setPopularity(banquet.getPopularity());
            temp.setIntegral(banquet.getIntegral());
            if (banquet.getMemberMap() != null) {
                for (CrossBanquetMember member : banquet.getMemberMap().values()) {
                    temp.addMemberRewardList(CrossBanquetPb.parseMemberRewardTempMsg(member));
                }
            }
            temp.setReward(banquet.getEndReward());
            temp.setMemCount(banquet.getUserCount());
            respMsg.addRewardList(temp);
        }
        respMsg.setRet(0);
        respMsg.setFillRobotNum(fillRobotNum);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_PARTY_REWARD, respMsg);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), pbMsg);

    }

    @Override
    public void sendData() {

    }
}
